require(['common', "coAjax"], function(common, coAjax) {

	function _upload_config(cb) {
		coAjax.put(appConfig.bus.set_config_url, {
			data: JSON.stringify(App.get("bus_loginer.config.notices")),
			type: "json",
			key: "notices"
		}, function(result) {
			App.set("bus_loginer", result.result);
			cb && cb();
		});
	}
	App.set("$Event.admin.notices.add", function(e) {
		var notices = App.get("bus_loginer.config.notices") || [];
		var new_notices = App.get("$Cache.admin.notices") || {};
		new_notices.content = jSouper.trim(new_notices.content);
		if (new_notices.content) {
			notices.push(new_notices);
			App.set("bus_loginer.config.notices", notices);
			_upload_config(function() {
				alert("success", "添加成功");
				App.set('$Cache.admin.notices', {});
			});
		} else {
			alert("warn", "公告内容不可为空")
		}
		_upload_config();
	});

	App.set("$Event.admin.notices.to_update", function(e, vm) {
		vm.set("_bak_content", vm.get("content"));
		vm.set("_bak_url", vm.get("url"));
		vm.set("$Private.$Cache.editing", true);
	});
	App.set("$Event.admin.notices.put_update", function(e, vm) {
		if (vm.get('content')) {
			_upload_config(function() {
				alert("success", "修改成功");
			});
			vm.set("$Private.$Cache.editing", false);
		} else {
			alert('warn', '公告栏不可为空')
		}
	});
	App.set("$Event.admin.notices.cancel_update", function(e, vm) {
		vm.set("content", vm.get("_bak_content"));
		vm.set("url", vm.get("_bak_url"));
		vm.set("$Private.$Cache.editing", false);
	});
	App.set("$Event.admin.notices.remove", function(e, vm) {
		vm.remove();
		_upload_config(function() {
			alert("success", "删除成功");
		});
	});
});